package org.i_plus.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.Assistance;
import org.i_plus.model.BlogBean;
import org.i_plus.model.HobbyBean;
import org.i_plus.model.MemberBean;
import org.i_plus.model.services.MemberService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class MypageServlet extends HttpServlet { 
private static final long serialVersionUID = 1L; 
    private MemberService service; 
    public void init() throws ServletException { 
        WebApplicationContext context = 
                WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext()); 
        this.service = (MemberService) context.getBean("MemberService");         
    } 
    @Override
    protected void doGet(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException { 
            request.setCharacterEncoding("UTF-8"); 
            String account=request.getParameter("account"); 

            	MemberBean bbean=service.select_by_account(account); 
                List<BlogBean> blogbean=service.select_blog_by_account(account); 
                request.setAttribute("blog",blogbean); 
                  
                if(bbean!=null)//從資料庫裡抓出來是m及f！在這裡判斷，然後show中文字傳到前端！ 
                { 
                    if(bbean.getSex().equals("m")){ 
                        request.setAttribute("bg", "男"); 
                    }else{ 
                        request.setAttribute("bg", "女"); 
                    } 
                    HobbyBean hbean=service.selectHobby(bbean.getAccount());//用登入後的account去抓"興趣"的bean！ 
                    Assistance ass=new Assistance();//new 這個class 
                    String blood=ass.howTypeBlood(bbean.getBlood());//呼叫方法，判斷血型！ 
                    request.setAttribute("blood", blood); 
                    int old=ass.howOldAreYou(bbean.getBdate());//呼叫方法！這方法是判斷幾歲！ 
                    request.setAttribute("old",old); 
                    String star=ass.howSignAreYou(bbean.getBdate());//呼叫方法！這方法是判斷什麼星座！ 
                    request.setAttribute("starSign", star); 
                    String hobbys=ass.hobbyTrans(hbean); 
                    request.setAttribute("hobby", hobbys); 
//                  Map<String,String> ttry=new HashMap<String,String>(); 
                    if(bbean.getPic1()!=null) 
                    request.setAttribute("temppicture1", Assistance.photoByteToImgString("image/jpeg", bbean.getPic1())); 
                    else
                    request.setAttribute("temppicture1",null); 
                    request.setAttribute("ttry",bbean); 
                    request.getRequestDispatcher("/View/IamMember.jsp"). 
                    forward(request, response); 
                    return; 
                      
                } 
                else
                { 
                    request.getRequestDispatcher("jsp"). 
                    forward(request, response); 
                    return; 
                } 
              
    }  
  
    protected void doPost(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException { 
        this.doGet(request, response); 
    } 
  
} 